<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>管理</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../assets/module/admin.css?v=315"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">用户名：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="输入用户名"/>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchBook" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddBook" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableBook" lay-filter="tableBook"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarBook">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delCar">停用车位</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delRoom">停用房屋</a>
    <a class="layui-btn layui-btn-xs" lay-event="room">房屋分配</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="car">车位分配</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="payment">缴费管理</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
</script>

<script type="text/html" id="modelPayment">
    <form id="modelPaymentForm" lay-filter="modelPaymentForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="payment_id" lay-verify="required" lay-filter="paymentCategory">
                    <option value="">请选择收费类型</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">费用</label>
            <div class="layui-input-block">
                <input name="value" class="layui-input" type="text" placeholder="输入费用"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitPayment" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="modelCar">
    <form id="modelCarForm" lay-filter="modelCarForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select name="type" lay-verify="required" lay-filter="carCategory">
                    <option value="">请选择供楼栋类型</option>
                    <option value="0">地上</option>
                    <option value="1">地下</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">空闲车位</label>
            <div class="layui-input-block">
                <select name="car_id" lay-verify="required">
                    <option value="">请选择空闲车位</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitCar" lay-submit>保存</button>
        </div>
    </form>
</script>

<script type="text/html" id="modelRoom">
    <form id="modelRoomForm" lay-filter="modelRoomForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">单元</label>
            <div class="layui-input-block">
                <select name="danyuan_id" lay-verify="required" lay-filter="parentCategory">
                    <option value="">请选择单元</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">空闲房间</label>
            <div class="layui-input-block">
                <select name="room_id" lay-verify="required">
                    <option value="">请选择空闲房间</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitRoom" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelBook">
    <form id="modelBookForm" lay-filter="modelBookForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入用户名" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="手机号" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="phone" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <select name="sex" lay-verify="required">
                    <option value="">请选择性别</option>
                    <option value="0">男</option>
                    <option value="1">女</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitBook" lay-submit>保存</button>
        </div>
    </form>
</script>
<script type="text/html" id="building_1">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.status}}" lay-skin="switch" disabled
           lay-text="正常|停用" {{d.status==1?'checked':''}}/>
</script>
<script type="text/html" id="building_2">
    <input type="checkbox" lay-filter="ckStateUser" value="{{d.sex}}" lay-skin="switch" disabled
           lay-text="男|女" {{d.sex==0?'checked':''}}/>
</script>
<!-- js部分 -->
<script type="text/javascript" src="../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../assets/js/common.js?v=315"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'tableX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tableX = layui.tableX;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableBook',
            url: '/api/getUsers',
            page: true,
            height: "full-100",
            toolbar: true,
            cellMinWidth: 100,
            cols: [
                [
                    {type: 'numbers', title: '#'}
                    ,{field: 'username', sort: true, title: '用户名'}
                    ,{field: 'phone', sort: true, title: '手机号'}
                    ,{field: 'sex', sort: true, templet: '#building_2', title: '性别'}
                    ,{field: 'car', sort: true, templet: function (d) {
                        if (d.car != null){
                            return d.car.name;
                        }else {
                            return '暂无'
                        }
                    }, title: '车位'}
                    ,{field: 'room', sort: true, templet: function (d) {
                        if (d.room != null){
                            return d.room.danyuan.name+d.room.name;
                        }else {
                            return '暂无'
                        }
                    }, title: '房屋'}
                    ,{align: 'center', toolbar: '#tableBarBook', title: '操作', minWidth: 510}
                ]
            ]
        });

        // 添加
        $('#btnAddBook').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchBook)', function (data) {
            insTb.reload({where: data.field,url:'/api/findUser',page : {curr : 1}}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableBook)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 停用
                doDel(data.id, data.username);
            } else if (layEvent === 'room') { // 分配房间
                showRoomModel(data);
            } else if (layEvent === 'car') { // 分配房间
                showCarModel(data);
            } else if (layEvent === 'delCar') { // 停用
                doDelCar(data.id);
            } else if (layEvent === 'delRoom') { // 停用
                doDelRoom(data.id);
            } else if (layEvent === 'payment') { // 停用
                showPaymentModel(data);
            }
        });
        function showPaymentModel(data) {
            admin.open({
                type: 1,
                title: '缴费',
                content: $('#modelPayment').html(),
                success: function (layero, dIndex) {
                    var option="";
                    $.ajax({
                        url: '/ajax/getAllPayments',
                        dataType: 'json',
                        type: 'get',
                        async:false,
                        success: function (res) {
                            if (res.code == 0){
                                for(var i in res.data){
                                    option += "<option value='"+res.data[i]['id']+"'>"+res.data[i]['name']+"</option>";
                                }
                            }
                            $("[name='payment_id']").append(option);
                            form.render()
                        }
                    })
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = '/api/fenpeiPayment';
                    // 回显数据
                    form.val('modelPaymentForm', data);
                    // 表单提交事件
                    form.on('submit(modelSubmitPayment)', function (data) {
                        layer.load(2);
                        $.ajax({
                            url:url,
                            type:"POST",
                            data:JSON.stringify(data.field),
                            contentType:"application/json; charset=utf-8",
                            dataType:"json",
                            success: function(res){
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb.reload({}, 'data');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        function showCarModel(data) {
            admin.open({
                type: 1,
                title: '分配车位',
                content: $('#modelCar').html(),
                success: function (layero, dIndex) {
                    form.on('select(carCategory)',function(res){
                        console.log(res.value)
                        var option="";
                        $.ajax({
                            url: '/ajax/getAllFreeCars?type='+res.value,
                            dataType: 'json',
                            type: 'get',
                            async:false,
                            success: function (res) {
                                if (res.code == 0){
                                    for(var i in res.data){
                                        option += "<option value='"+res.data[i]['id']+"'>"+res.data[i]['name']+"</option>";
                                    }
                                }
                                $("[name='car_id']").append(option);
                                form.render()
                            }
                        })
                    });

                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = '/api/fenpeiCar';
                    // 回显数据
                    form.val('modelCarForm', data);
                    // 表单提交事件
                    form.on('submit(modelSubmitCar)', function (data) {
                        layer.load(2);
                        $.ajax({
                            url:url,
                            type:"POST",
                            data:JSON.stringify(data.field),
                            contentType:"application/json; charset=utf-8",
                            dataType:"json",
                            success: function(res){
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb.reload({}, 'data');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }
        function showRoomModel(data) {
                admin.open({
                    type: 1,
                    title: '分配房屋',
                    content: $('#modelRoom').html(),
                    success: function (layero, dIndex) {
                        var option="";
                        $.ajax({
                            url: '/ajax/getAllDanyuans',
                            dataType: 'json',
                            type: 'get',
                            async:false,
                            success: function (res) {
                                if (res.code == 0){
                                    for(var i in res.data){
                                        option += "<option value='"+res.data[i]['id']+"'>"+res.data[i]['name']+"</option>";
                                    }
                                }
                                $("[name='danyuan_id']").append(option);
                                form.render()
                            }
                        })
                        form.on('select(parentCategory)',function(res){
                            console.log(res.value)
                            var option="";
                            $.ajax({
                                url: '/ajax/getAllFreeRooms?danyuan_id='+res.value,
                                dataType: 'json',
                                type: 'get',
                                async:false,
                                success: function (res) {
                                    if (res.code == 0){
                                        for(var i in res.data){
                                            option += "<option value='"+res.data[i]['id']+"'>"+res.data[i]['name']+"</option>";
                                        }
                                    }
                                    $("[name='room_id']").append(option);
                                    form.render()
                                }
                            })
                        });

                        $(layero).children('.layui-layer-content').css('overflow', 'visible');
                        var url = '/api/fenpeiRoom';
                        // 回显数据
                        form.val('modelRoomForm', data);
                        // 表单提交事件
                        form.on('submit(modelSubmitRoom)', function (data) {
                            layer.load(2);
                            $.ajax({
                                url:url,
                                type:"POST",
                                data:JSON.stringify(data.field),
                                contentType:"application/json; charset=utf-8",
                                dataType:"json",
                                success: function(res){
                                    layer.closeAll('loading');
                                    if (res.code == 200) {
                                        layer.close(dIndex);
                                        layer.msg(res.msg, {icon: 1});
                                        insTb.reload({}, 'data');
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            })
                            return false;
                        });
                    }
                });
            }
        // 显示表单弹窗
        function showEditModel(mBook) {
            if(mBook==null){
                admin.open({
                    type: 1,
                    title: '添加用户',
                    content: $('#modelBook').html(),
                    success: function (layero, dIndex) {
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');
                        var url = '/api/addUser';
                        // 回显数据
                        form.val('modelBookForm', mBook);
                        // 表单提交事件
                        form.on('submit(modelSubmitBook)', function (data) {
                            layer.load(2);
                            $.ajax({
                                url:url,
                                type:"POST",
                                data:JSON.stringify(data.field),
                                contentType:"application/json; charset=utf-8",
                                dataType:"json",
                                success: function(res){
                                    layer.closeAll('loading');
                                    if (res.code == 200) {
                                        layer.close(dIndex);
                                        layer.msg(res.msg, {icon: 1});
                                        insTb.reload({}, 'data');
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            })
                            return false;
                        });
                    }
                });
            }else{
                admin.open({
                    type: 1,
                    title: '修改用户',
                    content: $('#modelBook').html(),
                    success: function (layero, dIndex) {
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');
                        var url = '/api/updateUser';
                        //mBook && (mBook.roleId = mUser.roles[0].roleId);
                        // 回显数据
                        form.val('modelBookForm', mBook);
                        // 表单提交事件
                        form.on('submit(modelSubmitBook)', function (data) {
                            layer.load(2);
                            $.ajax({
                                url:url,
                                type:"POST",
                                data:JSON.stringify(data.field),
                                contentType:"application/json; charset=utf-8",
                                dataType:"json",
                                success: function(res){
                                    layer.closeAll('loading');
                                    if (res.code == 200) {
                                        layer.close(dIndex);
                                        layer.msg(res.msg, {icon: 1});
                                        insTb.reload({}, 'data');
                                    } else {
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            })
                            return false;
                        });
                    }
                });
            }

        }

        // 删除
        function doDel(id, name) {
            layer.confirm('确定要停用“' + name + '”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.get('/api/stopUser', {
                    id: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
// 删除
        function doDelCar(id) {
            layer.confirm('确定要停用车位吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.get('/api/stopCarByUserId', {
                    id: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
        function doDelRoom(id) {
            layer.confirm('确定要停用房屋吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.get('/api/stopRoomByUserId', {
                    id: id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({}, 'data');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
    });
</script>

</body>
</html>